<?php
declare(strict_types=1);

namespace App\Service;

use App\Model\Dao\RateSettleDisLogDao;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use App\Model\Dao\RateSettleDisDao;
use App\Exception\XZXValidationException;
use App\Constants\XZXResponseCode;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use App\Utils\DateUtils;
use Hyperf\Utils\Codec\Json;


class RateSettleDisService
{
    /**
     * @Inject()
     * @var RateSettleDisDao
     */
    private $RateSettleDisDao;

    /**
     * @Inject()
     * @var RateSettleDisLogDao
     */
    private $RateSettleDisLogDao;

    public function get($param)
    {
        $result = $this->RateSettleDisDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $result['content'] = json::decode($result['content'],true);

        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function add($param)
    {
        $data            =  [
            'id'            => CustomUtils::createUUID(),
            'name'          => $param['name'],
            // 'content'       => $param['content'],
            'remark'        => $param['remark'],
            'createTime'    => time(),
            'createBy'      => $param['createBy'],
            'updateBy'      => $param['createBy'],
        ];

        $content = $param['content'];
        
        $data['content'] = json::encode($content);        

        try {
            $this->RateSettleDisDao->insert($data);

            $log = $data;
            $log['id'] = CustomUtils::createUUID();
            $log['rateId'] = $data['id'];
            $log['createTime'] = time();
            $this->RateSettleDisLogDao->insert($log);

        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th->getMessage());
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::ADD_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function edit($param)
    {
        $info = $this->RateSettleDisDao->get($param['id']);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $id = $param['id'];
        $data = array();
        foreach($param as $k => $v){
            if($k == "id"){
                continue;
            }            
            if(isset($v)){
                $data[$k] = $v;
            }
        }

        if(isset($param['content'])){
            $content = $param['content'];
            $data['content'] = json::encode($content);
        }
        
        try {
            $this->RateSettleDisDao->update($id, $data);

            $log = $data;
            $log['id'] = CustomUtils::createUUID();
            $log['rateId'] = $id;
            $log['createTime'] = time();
            $log['createBy'] = $param['createBy'];
            $log['updateBy'] = $param['createBy'];
            $this->RateSettleDisLogDao->insert($log);

        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th->getMessage());
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::EDIT_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];        

        $total = $this->RateSettleDisDao->getCount($param);
        $list = $this->RateSettleDisDao->getList($param, $page, $limit);

        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            $v['content'] = json::decode($v['content'],true);
            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }

    public function getOption(array $param) {
        $data = $this->RateSettleDisDao->getOption($param);
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }

    public function log($param) {
        $result = $this->RateSettleDisLogDao->getWhereList(['rateId' => $param['id']], ['createTime' => 'desc']);
        foreach ($result as $k => $v) {
            if(isset($v['createTime'])){
                $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            }
            $v['content'] = json::decode($v['content'],true);
            $result[$k] = $v;
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

}